﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data.SqlClient;

namespace DatabaseInterface
{
    public class Connection
    {
        XmlDocument doc;
        XmlReader reader;
        XMLData.XMLData xmlDataReader;
        private SqlConnection cnx;
        private static Connection instance;

        private string initialCatalog;
        private string dataSource;
        private string userId;
        private string password;
        private bool windowsAuth;

        public Connection()
        {
            SqlConnectionStringBuilder cnxBuilder = new SqlConnectionStringBuilder();
            string path = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "Connection.xml");
            this.xmlDataReader = new XMLData.XMLData(path, "Connection.xml", "Conexion");

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            
            this.dataSource = xmlDataReader.GetDato("Parametros", "DataSource");
            builder.DataSource = this.dataSource;

            this.initialCatalog = xmlDataReader.GetDato("Parametros", "InitialCatalog");
            builder.InitialCatalog = this.initialCatalog;
            try
            {
                this.windowsAuth = Convert.ToBoolean(xmlDataReader.GetDato("Parametros", "WindowsAuth"));
            }
            catch
            {
                this.windowsAuth = false;
            }
            if (windowsAuth)
            {
                builder.IntegratedSecurity = windowsAuth;
            }
            else
            {
                this.userId = xmlDataReader.GetDato("Parametros", "UserID");
                builder.UserID = this.userId;

                this.password = xmlDataReader.GetDato("Parametros", "Password");
                builder.Password = this.password;
            }

            this.cnx = new SqlConnection(builder.ConnectionString);
        }
        public static Connection Singleton
        {
            get
            {
                if (instance == null)
                {
                    instance = new Connection();
                }
                return instance;
            }
        }
        public SqlConnection getConnection()
        {
            return Singleton.cnx;
        }
        public string Catalog
        {
            get { return this.initialCatalog; }
            set { this.initialCatalog = value; }
        }
        public string DataSource
        {
            get { return this.dataSource; }
            set { this.dataSource = value; }
        }
        public bool WindowsAuth
        {
            get { return this.windowsAuth; }
            set { this.windowsAuth = value; }
        }
        public string User
        {
            get { return this.userId; }
            set { this.userId = value; }
        }
        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }
        public void Save()
        {
            this.xmlDataReader.SetDato("Parametros", "DataSource", this.dataSource);
            this.xmlDataReader.SetDato("Parametros", "InitialCatalog", this.initialCatalog);
            if (windowsAuth)
            {
                this.xmlDataReader.SetDato("Parametros", "UserID", "");
                this.xmlDataReader.SetDato("Parametros", "Password", "");
            }
            else
            {
                this.xmlDataReader.SetDato("Parametros", "UserID", this.userId);
                this.xmlDataReader.SetDato("Parametros", "Password", this.password);
            }
            this.xmlDataReader.SetDato("Parametros", "WindowsAuth", this.windowsAuth.ToString());
        }
        //private void DatosConexion()
        //{
        //    doc = new XmlDocument();
        //    string path = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "conexion.xml");
        //    reader = XmlReader.Create(path);
        //    doc.Load(reader);
        //}
        //public string GetParametro(string parametro)
        //{
        //    String valor = "";
        //    XmlNode nodo = doc.SelectSingleNode("/Conexion/Parametros");
        //    valor = nodo[parametro].Attributes["valor"].Value;
        //    valor.Trim();
        //    return valor;
        //}

    }
}
